﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace HelloXNA.Component.Sprite
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DrawDemo : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch _spriteBatch;

        Texture2D _texture2D;


        public DrawDemo(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            // TODO: Add your update code here

            base.Update(gameTime);
        }

        protected override void LoadContent()
        {
            _spriteBatch = new SpriteBatch(Game.GraphicsDevice);
            _texture2D = Game.Content.Load<Texture2D>("Image/Son");
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.CornflowerBlue);

            _spriteBatch.Begin();
            // 再 0,0 点绘制一个图像 
            _spriteBatch.Draw(_texture2D, Vector2.Zero, Color.White);
            // 再 10,10 点绘制一个图像，该图像的宽为原始图像的 1/2，高为原始图像的 1/3
            _spriteBatch.Draw(_texture2D, new Rectangle(10, 10, _texture2D.Width / 2, _texture2D.Height / 3), Color.Green);
            // 绘制原始图像的上半部分 
            _spriteBatch.Draw(_texture2D, new Vector2(_texture2D.Width, 0), new Rectangle(0, 0, _texture2D.Width, _texture2D.Height / 2), Color.Red);
            // 将纹理放大 1.5 倍，并垂直翻转 
            _spriteBatch.Draw(_texture2D, new Vector2(_texture2D.Width * 2, 0), null, Color.Yellow, 0f, Vector2.Zero, 1.5f, SpriteEffects.FlipVertically, 0f);
            // 将纹理的中心点设置为原点（此时 0,0 点表现为纹理的中心点，而不是左上角点），顺时针旋转 45 度 
            _spriteBatch.Draw(_texture2D, new Vector2(_texture2D.Width / 2, _texture2D.Height + _texture2D.Height / 2), null, Color.Blue, (float)(Math.PI / 180) * 45, new Vector2(_texture2D.Width / 2, _texture2D.Height / 2), 1f, SpriteEffects.None, 0f);

            _spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
